import Common from '../../Common/common'
export default class Receive extends Laya.Dialog {
    public Arr: number[] = [5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100]
    public randomProp: number[] = [3, 4, 5, 6]
    public numList = [5, 15, '', 25, '', 105, 45, 165, 195]
    public isWhole = false
    public gemstoneNum
    public type 
    constructor() {
        super()
    }

    onOpened(args) {
        this.type = args.type
        if (args.type == "game") {
            this.gemstone.visible = true
            this.num.text = "X" + args.index
            this.gemstoneNum = args.index
            this.directButn.visible = true
            this.doubleButn.visible = true
            this.directButn.once(Laya.Event.CLICK, this, this.receiveGemstone)
            this.doubleButn.once(Laya.Event.CLICK, this, () => {
                Laya.stage.event("playSound", ["sound/tap.mp3"])
                console.log("看视频")
                Common.lastRewardedVideoEventName = "receiveDouble"
                Laya.stage.event("showRewardedVideoAd")
            })
        } else {
            Common.getRandomAd(0.65);
            this.changeData(args.index)
        }
        this.btnEvent()
    }

    btnEvent() {
        Laya.stage.on("receiveDouble", this, this.receiveDouble)
    }

    /**
     * 随机钻石或者道具
     */
    randomReward() {
        let random = Math.random()
        let type = random <= 0.1 ? 0 : 1
        return type
    }

    /**
     * 获取随机数
     */
    getRandom(Arr: number[]): number {
        var n = Math.floor(Math.random() * Arr.length + 1) - 1;
        return Arr[n]
    }

    /**
     * 改变领取内容
     */
    changeData(index) {
        if (index == 3 || index == 5) {
            let type = this.randomReward()
            if (type == 0) {
                let prop = this.getRandom(this.randomProp)
                this.num.visible = false
                this.getChildByName("character" + prop).visible = true
                this.receiveButn.visible = true
                this.receiveButn.on(Laya.Event.CLICK, this, this.receiveProp)
            } else {
                this.gemstone.visible = true
                this.num.text = "X" + this.getRandom(this.Arr)
                this.gemstoneNum = this.getRandom(this.Arr)
                this.directButn.visible = true
                this.doubleButn.visible = true
                this.directButn.once(Laya.Event.CLICK, this, this.receiveGemstone)
                this.doubleButn.once(Laya.Event.CLICK, this, () => {
                    Laya.stage.event("playSound", ["sound/tap.mp3"])
                    console.log("看视频")
                    Common.lastRewardedVideoEventName = "receiveDouble"
                    Laya.stage.event("showRewardedVideoAd")
                })
            }
        } else {
            this.gemstone.visible = true
            this.num.text = "X" + this.numList[index - 1]
            this.gemstoneNum = this.numList[index - 1]
            this.directButn.visible = true
            this.doubleButn.visible = true
            if(index == 7 || index == 9){
                this.isWhole = true
            }
            this.directButn.once(Laya.Event.CLICK, this, this.receiveGemstone)
            this.doubleButn.once(Laya.Event.CLICK, this, () => {
                Laya.stage.event("playSound", ["sound/tap.mp3"])
                console.log("看视频")
                Common.lastRewardedVideoEventName = "receiveDouble"
                Laya.stage.event("showRewardedVideoAd")
            })

        }

    }



    /**
     * 领取双倍
     */
    receiveDouble() {
        
        let double = this.gemstoneNum * 2
        let gemstone = Common.getCacheItem('gemstone')
        Laya.stage.event("ShowMsg", "领取奖励成功")
        if (Common.getCacheItem('gemstone') == null) {
            Common.setCacheItem('gemstone', double)
        } else {
            Common.setCacheItem('gemstone', gemstone += double)
        }
        if (this.isWhole) {
            this.gemstone.visible = false
            this.directButn.visible = false
            this.num.visible = false
            this.doubleButn.visible = false
            let prop = this.getRandom(this.randomProp)
            this.getChildByName("character" + prop).visible = true
            this.receiveButn.visible = true
            this.receiveButn.on(Laya.Event.CLICK, this, this.receiveProp,[prop])
        } else {
            this.close()
            if(this.type == "game"){
                Laya.stage.event("gameOver",[double])
            }
        }
    }

    /**
     * 领取钻石奖励
     */
    receiveGemstone() {
        Laya.stage.event("playSound", ["sound/tap.mp3"])
        let double = this.gemstoneNum
        let gemstone = Common.getCacheItem('gemstone')
        Laya.stage.event("ShowMsg", "领取奖励成功")
        if (Common.getCacheItem('gemstone') == null) {
            Common.setCacheItem('gemstone', double)
        } else {
            Common.setCacheItem('gemstone', gemstone += double)
        }
        if (this.isWhole) {
            this.gemstone.visible = false
            this.directButn.visible = false
            this.num.visible = false
            this.doubleButn.visible = false
            let prop = this.getRandom(this.randomProp)
            this.getChildByName("character" + prop).visible = true
            this.receiveButn.visible = true
            this.receiveButn.on(Laya.Event.CLICK, this, this.receiveProp,[prop])
        } else {
            this.close()
            if(this.type == "game"){
                Laya.stage.event("gameOver",[double])
            }
        }
    }

    /**
     * 领取道具奖励
     */
    receiveProp(prop: number) {
        
        Laya.stage.event("playSound", ["sound/tap.mp3"])
        if (Common.getCacheItem("buyed" + prop) == null || Common.getCacheItem("buyed" + prop) == "0") {
            Common.setCacheItem("buyed" + prop, "1")
            Laya.stage.event("ShowMsg", "领取奖励成功")
        } else {
            Laya.stage.event("ShowMsg", "已有该道具，无法领取")
        }
        this.close()
    }

    onClosed(){
        this.receiveButn.off(Laya.Event.CLICK,this);
    }
}